package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//分类管理页面

@RestController
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;


    //添加菜品dish以及dishflavor表
    @CacheEvict(value = "dish_", key = "#dto.getCategoryId()")
    @PostMapping
    @ApiOperation("添加菜品dish以及dishflavor表")
    public Result insertDish(@RequestBody DishDTO dto) {
        dishService.insertDishService(dto);
        return Result.success();

    }


    //查询菜品并进行名称分类状态进行模糊查询
    @GetMapping("/page")
    @ApiOperation("查询菜品并进行名称分类状态进行模糊查询")
    public Result selectDishService(DishPageQueryDTO dto) {
        log.info("菜品{}+状态{}", dto.getCategoryId(), dto.getStatus());
        PageResult pageResult = dishService.selectDishService(dto);
        return Result.success(pageResult);
    }


    //根据id删除菜品（- 可以一次删除一个菜品，也可以批量删除菜品
    //- 起售中的菜品不能删除
    //- 被套餐关联的菜品不能删除
    //- 删除菜品后，关联的口味数据也需要删除掉）
    @CacheEvict(value = "dish_",allEntries = true)
    @DeleteMapping
    @ApiOperation("根据id删除菜品")
    public Result deleteDishService(Integer[] ids) {
        log.info("删除前端传过来了id" + ids.length + "个");
        dishService.deleteDishService(ids);
        return Result.success();
    }


    // 进行修改先查询菜品信息包含dish表和dish_flavor表
    @GetMapping("/{dishId}")
    @ApiOperation("进行修改先查询菜品信息包含dish表和dish_flavor表")
    public Result selectDishAndDishFlavor(@PathVariable Integer dishId) {
        DishVO dishVO = dishService.selectDishAndDishFlavorServise(dishId);
        return Result.success(dishVO);

    }


    //修改菜品包含dish表和dish_flavor表，修改dish_flavor表先给他删除再添加就变样成了修改

    @CacheEvict(value = "dish_", key = "#dto.getCategoryId()")
    @PutMapping
    @ApiOperation("修改菜品")
    public Result updateDish(@RequestBody DishDTO dto) {
        dishService.updateDishService(dto);
        return Result.success();
    }


   /** 在套餐管理添加菜品里查询到套餐下菜品（查询后续添加）*/
    @GetMapping("/list")
    @ApiOperation("在套餐管理添加菜品里查询到套餐下菜品")
    public Result selectCategoryDish(Integer categoryId){
        List<Dish> dishes = dishService.selectCategoryDishService(categoryId);
        return Result.success(dishes);


    }


    /**修改菜品售卖状态*/
    @CacheEvict(value = "dish_", allEntries = true)
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Integer status, Long id) {
        Integer id1 = Math.toIntExact(id);

        dishService.updateDishStatusService(status, id1);
        return Result.success();
    }


}
